<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>文字识别</title>
    <link href="/css/bootstrap.min.css?v=3.3.6" rel="stylesheet"/>
    <link href="/css/font-awesome.css?v=4.4.0" rel="stylesheet"/>
    <link href="/css/animate.css" rel="stylesheet"/>
    <link href="/css/style.css?v=4.1.0" rel="stylesheet"/>
    <link href="${ctxPath}/css/layui.css" rel="stylesheet" media="all">
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content  animated fadeInRight">
    <div class="row">
        <div class="ibox">
            <div class="ibox-title">
                <h5>图片上传</h5>
            </div>
            <div class="ibox-content">
                <div class="layui-upload">
                    <div class="layui-upload-list">
                        <img width="200px" height="200px" src="${ctxPath}/img/webuploader.png" id="imageShow" />
                        <p id="messText"></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="ibox">
            <div class="ibox-title">
                <h5>转换结果</h5>
            </div>
            <div class="ibox-content">
                <div class="row row-lg">
                    <div class="col-sm-12">
                        <div class="alert alert-info">
                            <p id="orcResult">...</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<!-- 全局js -->
<script src="/js/jquery.min.js?v=2.1.4"></script>
<script src="/js/bootstrap.min.js?v=3.3.6"></script>

<!-- 自定义js -->
<script src="/js/content.js?v=1.0.0"></script>

<!-- layui upload -->
<script src="${ctxPath}/js/layui.js" charset="utf-8"></script>

<script type="text/javascript">
    layui.use('upload', function(){
        var upload = layui.upload;
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#imageShow'
            ,url: '/upload/'
            ,data: {type:0}
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#imageShow').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.success){
                    getFullName(res.data);
                }else{
                    return layer.msg('上传失败');
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#messText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
    });

    function getFullName(file) {
        $.ajax({
            type: "POST",
            url: "/ocr/upload",
            data:{file:file},
            beforeSend: function () {
                layer.load(0, {shade: false});
            },
            success: function(ret){
                if(ret.success){
                    var resultStr = ret.data;
                    $( '#orcResult' ).html(resultStr);
                }
                layer.closeAll('loading');
            }
        });
    }
</script>

</body>

</html>
